<!--  -->
<template>
  <div class="account">
    <h1>账号信息</h1>
    <el-form label-width="80px">
      <el-form-item style="width: 500px" label="昵称">
        <el-input v-model="userList.username"></el-input>
      </el-form-item>
      <el-form-item style="width: 500px" label="密码">
        <el-input type="password" v-model="userList.password"></el-input>
      </el-form-item>
      <el-form-item style="width: 500px" label="头像">
        <img :src="userList.header" alt="" class="headimag" />
        <OssUpload v-model="userList.header"></OssUpload>
      </el-form-item>
      <el-form-item style="width: 500px" label="手机号">
        <el-input v-model="userList.mobile"></el-input>
      </el-form-item>
      <!-- <el-form-item label="注册时间">
        <el-input v-model="userList.time"></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import OssUpload from "../OssUpload";
import cookie from "js-cookie";
export default {
  name: "Account",
  data() {
    return {
      userList: {
        username: "",
        password: "",
        header: "",
        mobile: "",
      },
      fileList: [],
    };
  },

  components: {
    OssUpload,
  },
  created() {
    this.getUserList();
  },

  methods: {
    // 提交保存
    async onSubmit() {
      const { data: res } = await this.$http.post(
        "/dqxmall/userinfo/update",
        this.userList
      );
      //  console.log(res)
      if (res.msg === "success") {
        // 获取返回的用户信息放到cookie中userInfo1
        // console.log(cookie.get("userInfo"))
        cookie.set("userInfo", JSON.stringify(this.userList), { domain: "localhost" });
        // console.log(cookie.get("userInfo"))
        this.$store.dispatch("setUserInfo", this.userList);
        this.getUserList();
        this.$message.success("保存成功");
      }
    },
    // 获取用户信息
    getUserList() {
      // this.userList=  this.$store.getters.getUserInfo
      // 从cookie获取用户信息
      var userStr = cookie.get("userInfo");
      // 把字符串转换json对象（js对象）
      if (userStr) {
        this.userList = JSON.parse(userStr);
        // console.log(this.userList)
      }
    },
  },
};
</script>
<style lang="less" scoped>
h1 {
  font-size: 25px;
  font-weight: 700;
  margin: 20px;
  text-align: center;
}
.headimag {
  height: 70px;
  width: 70px;
}
</style>
